package com.three.service.impl;

import com.three.entity.Menu;
import com.three.entity.Staff;
import com.three.mapper.MenuMapper;
import com.three.service.MenuService;
import com.three.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private HttpSession session;
    @Override
    public R getMenuByStaffId() {
        Staff staff = (Staff) session.getAttribute("staff");//获取当前登录用户
        Integer staffid = staff.getStaffid();//获取当前登录用户id
        List<Menu> menus = menuMapper.getMenuByStaffId(staffid);//根据当前登录用户id获取到的所有菜单
        List<Menu> firstMenus = new ArrayList<>();//存放一级菜单
        //遍历所有菜单,得到一级菜单
        for (Menu menu : menus) {
            if (menu.getMpid() == 0) {
                firstMenus.add(menu);
            }
        }
        //遍历一级菜单和所有菜单,得到二级菜单并将二级菜单放入一级菜单中
        for (Menu menu : firstMenus) {
            List<Menu> secondMenus = new ArrayList<>();
            for (Menu menu1 : menus) {
                //判断二级菜单的父菜单id是否等于一级菜单的id
                if (menu1.getMpid()==menu.getMid()) {
                    secondMenus.add(menu1);
                }
            }
            //将二级菜单放入一级菜单中
            menu.setChildren(secondMenus);
        }
        return R.ok(firstMenus);
    }

    @Override

    public R selectByRoleId(Integer roleid) {
        //获取所有菜单
        List<Menu> menus = menuMapper.selectAll();
        //将所有的菜单分为一级目录和二级目录
        List<Menu> firstMenus = new ArrayList<>();//存放一级菜单
        //遍历所有菜单,得到一级菜单
        for (Menu menu : menus) {
            if (menu.getMpid() == 0) {
                firstMenus.add(menu);
            }
        };
        //遍历一级菜单和所有菜单,得到二级菜单并将二级菜单放入一级菜单中
        for (Menu menu : firstMenus) {
            List<Menu> secondMenus = new ArrayList<>();
            for (Menu menu1 : menus) {
                //判断二级菜单的父菜单id是否等于一级菜单的id
                if (menu1.getMpid()==menu.getMid()) {
                    secondMenus.add(menu1);
                }
            }
            //将二级菜单放入一级菜单中
            menu.setChildren(secondMenus);
        }
        //通过角色id获取菜单id
        List<Integer> menuIds = menuMapper.selectByRoleId(roleid);
        //将一级目录和菜单id放入map中
        Map<String, Object> map = new HashMap<>();
        map.put("firstMenus", firstMenus);
        map.put("menuIds", menuIds);
        return R.ok(map);
    }

    @Override
    @Transactional
    public R updateRoleMenu(Integer roleid, Integer[] checkedKeys) {
        //根据roleid删除树状图数据
        menuMapper.deleteByRoleId(roleid);
        //遍历checkedKeys,根据获取的id将数据插入数据库
        for (Integer key : checkedKeys) {
            menuMapper.insertRoleMenu(roleid, key);
        }
        return R.ok();
    }
}
